package com.atguigu.gmall.weball.controller;


import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.feign.order.OrderFeignClient;
import com.atguigu.gmall.feign.seckill.SeckillFeignClient;
import com.atguigu.gmall.feign.user.UserFeignClient;
import com.atguigu.gmall.model.activity.SeckillGoods;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.model.user.UserAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class SeckillController {

    @Autowired
    SeckillFeignClient seckillFeignClient;

    @Autowired
    OrderFeignClient orderFeignClient;

    @Autowired
    UserFeignClient userFeignClient;


    //秒杀商品列表页
    @RequestMapping("/seckill.html")
    public String seckillPage(Model model){

        // item
        //  (skuId、skuDefaultImg、
        //      skuName、costPrice、
        //      price、num、stockCount、stockCount）

        Result<List<SeckillGoods>> goods = seckillFeignClient.getTodaySeckillGoods();
        //查出当天参与秒杀的所有商品
        //查询数据库
        model.addAttribute("list",goods.getData());
        return "seckill/index";
    }

    //秒杀商品详情页
    //seckill/60.html

    @GetMapping("/seckill/{skuId}.html")
    public String seckillDetail(@PathVariable("skuId") Long skuId,
                                Model model){


        Result<SeckillGoods> detail = seckillFeignClient.getSeckillGoodsDetail(skuId);

        model.addAttribute("item",detail.getData());
        return "seckill/item";
    }



    ///seckill/queue.html?skuId=56&skuIdStr=b39583c345e890f00b2ed517b1149e2f
    //秒杀排队页
    @GetMapping("/seckill/queue.html")
    public String queuePage(@RequestParam("skuId") Long skuId,
                            @RequestParam("skuIdStr") String code,
                            Model model){


        model.addAttribute("skuId",skuId);
        model.addAttribute("skuIdStr",code);
        //来到秒杀商品排队页
        return "seckill/queue";
    }


    //seckill/trade.html
    @GetMapping("/seckill/trade.html")
    public String seckillOrderConfirmPage(@RequestParam("code")String code,
                                          @RequestParam("skuId") Long skuId,
                                          Model model){

        Result<OrderInfo> info = orderFeignClient.getSeckillOrderInfo(code, skuId);
        if(info.isOk()){
            OrderInfo data = info.getData();
            model.addAttribute("detailArrayList",data.getOrderDetailList());
            model.addAttribute("totalNum","1");
            model.addAttribute("totalAmount",data.getTotalAmount());
            Result<List<UserAddress>> address = userFeignClient.getUserAddress();
            model.addAttribute("userAddressList",address.getData());
        }

        //detailArrayList
        //totalNum
        //totalAmount
        //userAddressList
        return "seckill/trade";
    }


}
